<?php

namespace common\models\work;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%company_department}}".
 *
 * @property int $id
 * @property int $company_id 单位ID
 * @property string $name 部门名称
 * @property string $mark 备注
 * @property int $listorder 展示排序
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class CompanyDepartment extends \common\db\ActiveRecord {

    use \common\traits\Constants;

    /**
     * 状态
     */
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%company_department}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['company_id', 'status', 'listorder', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name'], 'required'],
            [['name', 'mark'], 'string', 'max' => 50],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'company_id' => '单位ID',
            'name' => '治理责任公司',
            'mark' => '备注',
            'listorder' => '展示排序',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public static function findByCompanyId($companyId = 0) {
        return static::find()->active(static::STATUS_ACTIVE)->andWhere([
            'company_id' => $companyId
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public static function getOptionsList($fill = false) {
        $data = static::find()->active(static::STATUS_ACTIVE)->orderBy([
            'listorder' => SORT_ASC
        ])->all();

        $departments = [];
        foreach ($data as $item) {
            $departments[] = [
                'id' => $item->id,
                'name' => $item->name,
            ];
        }
        if (!empty($departments) && $fill !== false) {
            if (is_array($fill)) {
                $departments[] = $fill;
            } else {
                $departments[] = [
                    'id' => 0,
                    'name' => '其他',
                ];
            }
        }
        return $departments;
    }

}
